//
//  RJTextField.m
//  RJTextField
//
//  Created by ChangRJey on 2017/7/28.
//  Copyright © 2017年 RenJiee. All rights reserved.
//

#import "RJTextField.h"
#define TextColor  [UIColor colorWithHexString:@"454545"];
@interface RJTextField ()<UITextFieldDelegate>


/** 左侧图标 */
@property (nonatomic, strong) UIImageView * leftIcon;
/** 上浮的占位文本 */
@property (nonatomic, strong) UILabel * headerPlaceLabel;
/** 字数限制文本 */
@property (nonatomic, strong) UILabel * lengthLabel;
/** 底部分割线 */
@property (nonatomic, strong) UIView * bottomLine;
/** 错误提示信息 */
@property (nonatomic, strong) UILabel * errorLabel;

@end

@implementation RJTextField

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
//        self.textColor = [UIColor colorWithHexString:@"454545"];
//        self.lineDefaultColor = [UIColor colorWithHexString:@"cccccc"];
//        self.lineSelectedColor = [UIColor colorWithHexString:@"2c9ae8"];
       
    }
    return self;
}

#pragma mark -------------------- 在layoutSubviews方法中设置子控件的frame --------------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.textField.frame = CGRectMake(0, 15, self.width, 30);
    [self addSubview:self.textField];

    self.headerPlaceLabel.frame = CGRectMake(0, CGRectGetMinY(self.textField.frame)-15, 100, 15);
    [self addSubview:self.headerPlaceLabel];
    
    self.bottomLine.frame = CGRectMake(0, self.height-1, self.width, 1);
    
//    self.bottomLine.backgroundColor = self.lineSelectedColor;
    [self addSubview:self.bottomLine];
    
}

#pragma mark -------------------- 懒加载控件 --------------------
- (UITextField *) textField{
    if(!_textField){
        _textField = [UITextField new];
        _textField.backgroundColor = [UIColor clearColor];
        _textField.textColor = TextColor;
        _textField.font = FONT(14);
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.placeholder = self.placeholder;
//    _textField.clearButtonMode=UITextFieldViewModeWhileEditing;
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

- (UILabel *) headerPlaceLabel{
    if(!_headerPlaceLabel){
        _headerPlaceLabel = [UILabel new];
        _headerPlaceLabel.backgroundColor = [UIColor clearColor];
        _headerPlaceLabel.textColor = [UIColor colorWithHexString:@"2c9ae8"];
        _headerPlaceLabel.textAlignment = NSTextAlignmentLeft;
        _headerPlaceLabel.font = FONT(13);
        _headerPlaceLabel.text = self.textField.placeholder;
        _headerPlaceLabel.alpha = 0.0;
    }
    return _headerPlaceLabel;
}


- (UIView *) bottomLine{
    if(!_bottomLine){
        _bottomLine = [UIView new];
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
//        _bottomLine.backgroundColor = [UIColor redColor];
    }
    return _bottomLine;
}



#pragma mark -------------------- UITextFieldDelegate --------------------
- (void)textFieldEditingChanged:(UITextField *)sender
{
//    if (sender.text.length > self.maxLength) {
//        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//            self.errorLabel.alpha = 1.0;
//            self.errorLabel.textColor = self.lineWarningColor;
//            self.bottomLine.backgroundColor = self.lineWarningColor;
//            self.lengthLabel.textColor = self.lineWarningColor;
//            self.textField.textColor = self.lineWarningColor;
////            self.placeHolderLabel.textColor = self.lineWarningColor;
//        } completion:nil];
//    }else{
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//            self.errorLabel.alpha = 0.0;
            self.bottomLine.backgroundColor = [UIColor colorWithHexString:@"2c9ae8"];
//            self.lengthLabel.textColor = self.textLengthLabelColor;
            self.textField.textColor = TextColor;
            //self.placeHolderLabel.textColor = self.placeHolderLabelColor;
        } completion:nil];
//    }
//    self.lengthLabel.text = [NSString stringWithFormat:@"%zd/%zd",sender.text.length,self.maxLength];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self setPlaceHolderLabelHidden:NO];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [self setPlaceHolderLabelHidden:YES];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self endEditing:YES];
    
    [self setPlaceHolderLabelHidden:YES];
    
    return YES;
}

#pragma mark -------------------- 占位符提示 --------------------
- (void)setPlaceHolderLabelHidden:(BOOL)isHidden
{
    if (isHidden) {
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.headerPlaceLabel.alpha = 0.0f;
//        self.headerPlaceLabel.textColor = [UIColor colorWithHexString:@"cccccc"];
//            self.textField.alpha = 0.0f;
            self.textField.placeholder = self.placeholder;
            self.bottomLine.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
        } completion:nil];
    }else{
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.headerPlaceLabel.alpha = 1.0f;
            
//        self.headerPlaceLabel.textColor = [UIColor colorWithHexString:@"2c9ae8"];
        self.headerPlaceLabel.text = self.placeholder;
        self.textField.placeholder = @"";
        self.bottomLine.backgroundColor = [UIColor colorWithHexString:@"2c9ae8"];
        } completion:nil];
    }
}


@end
